const Service = require("egg").Service

class ArticleService extends Service {
  // 需要联表查询这首歌对应的用户信息
  async getList(page,pageSize){
    let start = parseInt((page-1)*pageSize)
    // let sql = `SELECT article.* ,user.coverPictureUrl as userPic,
    // user.musicCount,user.musicPlayCount,user.nickname,user.type 
    // FROM article INNER JOIN user ON article.userId = user.id
    // limit ? , ?`
    // let data = await this.app.mysql.query(sql,[start,pageSize])



    const userModel = this.ctx.model.User
    const temp = this.ctx.model.Article

 

    userModel.hasMany(temp);
    temp.belongsTo(userModel);


    let offset = parseInt((page-1)*pageSize)

    
    const data = await temp.findAndCountAll(
      { 
        include: userModel,
        offset: offset, 
        limit: pageSize ,
      }
      )

      

      data.rows.forEach((item) => {
        item.coverUrlList = item.coverUrlList.split(",")
      });


    return data
  }
  async getInfo(id){
    let sql = `SELECT article.* ,user.coverPictureUrl as userPic,
    user.musicCount,user.musicPlayCount,user.nickname,user.type 
    FROM article INNER JOIN user ON article.userId = user.id
    where article.id = ?`
    let data = await this.app.mysql.query(sql,[id])

    if(!data){
      throw new Error("文章信息不存在")
    }
    return data
  }
}

module.exports = ArticleService
